<template>
  <page-meta :root-font-size="fontsize" class="top-service">
    <xlNavBar
      title="置顶服务"
      :customJump="toManage"
      bgc="transparent"
    ></xlNavBar>
    <image
      src="https://canyinxinxi.cn/dist_img/WechatIMG5143.jpg"
      class="ts-bkg"
    />
    <image
      src="https://canyinxinxi.cn/system_work/top-info.png"
      class="ts-top"
      mode="aspectFill"
    />

    <div class="ts-content">
      <div class="c-wrapper">
        <div class="title_wrap">
          <text class="t_title">选择置顶时间（单选）</text>
          <text class="s_title">{{ dueTime }}到期</text>
        </div>
        <div class="radio_wrap">
          <div
            @click="handleCheckTime(item)"
            class="item"
            :class="[selectTime === item.value ? 'active' : '']"
            v-for="(item, index) in timeList"
            :key="index"
          >
            {{ item.name }}
            <img
              v-if="selectTime === item.value"
              class="image"
              src="./image/radio-active.png"
              mode="scaleToFill"
            />
          </div>
        </div>
      </div>
    </div>
    <footer class="footer">
      <div class="left">
        共需
        <text class="num">{{ integralAll }}</text>
        积分
      </div>
      <div class="right" @click="submit">确认置顶</div>
    </footer>
  </page-meta>
</template>

<script>
import { mapActions } from "vuex";
export default {
  name: "TopService",
  data() {
    let time = new Date().getTime() + 24 * 3600 * 1000;
    return {
      fontsize: "",
      storeId: "",
      city: "",
      timeList: [
        {
          name: "1天（24小时）",
          value: 1
        },
        {
          name: "2天",
          value: 2
        },
        {
          name: "3天",
          value: 3
        },
        {
          name: "7天",
          value: 7
        },
        {
          name: "15天",
          value: 15
        },
        {
          name: "30天",
          value: 30
        }
      ],
      userInfo: null,
      selectTime: 1,
      dueTime: this.$date.timestampToTime(time, 16),
      integralAll: 3,
      integralNeed: 0,
      showIntegralRecharge: false,
      showTopSuccess: false
    };
  },
  onLoad(options) {
    let designWidth = 37.5;
    uni.onWindowResize((res) => {
      this.fontsize =
        Math.floor((1.5 * res.size.windowWidth) / designWidth) + "px";
    });
    uni.getSystemInfo({
      success: (res) => {
        this.fontsize =
          Math.floor((1.5 * res.screenWidth) / designWidth) + "px";
      }
    });

    if (options.id) {
      this.storeId = options.id;
      this.city = options.city;
    }
  },
  methods: {
    ...mapActions("user", ["setUserInfo"]),
    handleCheckTime(item) {
      this.selectTime = item.value;
      let time = new Date().getTime() + item.value * 24 * 3600 * 1000;
      this.dueTime = this.$date.timestampToTime(time, 16);
      this.integralAll = item.value * 3;
    },
    async submit() {
      await this.getUserIntegral();
      if (this.userInfo.integral < this.integralAll) {
        this.showIntegralRecharge = true;
        this.integralNeed = this.integralAll - this.userInfo.integral;
      } else {
        let params = {
          user_id: this.$getStoraged("user_id")
            ? JSON.parse(this.$getStoraged("user_id"))
            : "",
          purpose_id: this.storeId * 1,
          type: 13,
          total_date: this.selectTime,
          integral: this.integralAll
        };
        let res = await this.$http.requestSync("/campaigns/create", params);
        if (res.code == 200) {
          this.setUserInfo();
          this.showTopSuccess = true;
        } else {
          this.$layer.message(res.message);
        }
      }
    },
    closeIntegralRecharge() {
      this.showIntegralRecharge = false;
    },
    async getUserIntegral() {
      let res = await this.$http.requestSync("/user/info");
      if (res.code === 200) {
        this.userInfo = res.data.user_info;
        this.$setStoraged("userInfo", res.data.user_info);
      } else {
        uni.showToast({
          title: res.message,
          icon: "none",
          duration: 2000
        });
      }
    },
    toManage() {
      this.$redirectTo("/shopPackage/manageStore");
    }
  }
};
</script>

<style scoped lang="scss">
.top-service {
  width: 100vw;
  .ts-bkg {
    display: flex;
    align-items: center;
    width: 104%;
    height: 500px;
    padding: 0;
    position: fixed;
    top: -20px;
    left: -4%;
    z-index: -1;
  }
  .ts-top {
    margin-top: 20px;
    width: 100%;
    height: 340px;
  }
  .ts-content {
    width: 100%;
    background: linear-gradient(180deg, #fff5ed 0%, #f7f7f7 100%);
    border-radius: 14px 14px 0px 0px;
    display: table;
    position: relative;
    top: -50px;
    margin-bottom: 80px;
    .c-wrapper {
      width: 90%;
      background: #ffffff;
      border-radius: 10px;
      margin: 15px 18px 10px;
      .title_wrap {
        display: flex;
        justify-content: space-between;
        align-items: center;
        box-sizing: border-box;
        padding: 10px 18px;
        margin-top: 10px;
        .t_title {
          font-size: 14px;
          font-family: PingFang SC-Semibold, PingFang SC;
          font-weight: 600;
          color: #333333;
        }
        .s_title {
          font-size: 12px;
          font-family: PingFang SC-Regular, PingFang SC;
          font-weight: 400;
          color: #999999;
        }
      }
      .ul {
        display: flex;
        width: 100%;
        box-sizing: border-box;
        padding: 10px 18px;
        .li {
          width: 78px;
          height: 32px;
          background: #f0f0f0;
          border-radius: 4px 4px 4px 4px;
          margin-right: 10px;
          font-size: 14px;
          font-family: PingFang SC-Regular, PingFang SC;
          font-weight: 400;
          color: #999999;
          text-align: center;
          line-height: 32px;
        }
        .active {
          color: #ffffff;
          background: #ff8822;
        }
      }
      /deep/.u-radio-group {
        display: flex;
        flex-wrap: wrap;
        margin: 0 auto;
        box-sizing: border-box;
        padding: 10px 18px;
        justify-content: space-around;
        .u-radio {
          width: 20%;

          font-family: PingFang SC-Regular, PingFang SC;
          font-weight: 400;
          color: #ff8822;
          .u-radio__text {
            font-size: 12px !important;
          }
        }
        .u-radio:nth-child(3n + 1) {
          width: 37%;
        }
      }
    }

    .radio_wrap {
      display: flex;
      width: 100%;
      flex-wrap: wrap;
      justify-content: space-between;
      box-sizing: border-box;
      padding: 0 18px;
      .item {
        width: 100px;
        height: 42px;
        background: #f6f6f6;
        border-radius: 5px 5px 5px 5px;
        font-size: 14px;
        font-weight: 400;
        color: #999999;
        text-align: center;
        line-height: 42px;
        margin-bottom: 12px;
        position: relative;
        .image {
          position: absolute;
          right: 0;
          bottom: 0px;
          width: 19px;
          height: 19px;
        }
      }
      .active {
        background-color: rgba(255, 136, 34, 0.1);
        border: 1px solid #ff8822;
      }
    }
  }
  .footer {
    position: fixed;
    width: 100%;
    height: 79px;
    bottom: 0;
    background: #fff;
    display: flex;
    justify-content: space-between;
    align-items: center;
    box-sizing: border-box;
    padding: 0 18px;
    .left {
      font-size: 14px;
      font-family: PingFang SC-Regular, PingFang SC;
      font-weight: 400;
      color: #333333;
      .num {
        color: #ffa222;
        padding: 0 6px;
        font-weight: 600;
      }
    }
    .right {
      width: 138px;
      height: 42px;
      background: linear-gradient(122deg, #ffa222 0%, #ff8822 100%);
      border-radius: 21px 21px 21px 21px;
      font-size: 16px;
      font-family: PingFang SC-Semibold, PingFang SC;
      font-weight: 600;
      color: #ffffff;
      text-align: center;
      line-height: 42px;
      border-top: 1px solid #e2dfdf;
    }
  }
}
</style>
